package com.wyf.android.advance.test

import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wyf.android.advance.R
import com.wyf.android.baselib.util.inflate

class TestAdapter : RecyclerView.Adapter<TestAdapter.TestViewHolder>() {

    private var mList: ArrayList<TestViewData>? = null

    inner class TestViewHolder(val itemRootView: TextView) : RecyclerView.ViewHolder(itemRootView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        return TestViewHolder(parent.inflate(R.layout.test_item) as TextView)
    }

    override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
        holder.itemRootView.text = mList?.get(position)?.toString() ?: ""
    }


    override fun getItemCount(): Int {
        return mList?.size ?: 0
    }

    fun setData(list: ArrayList<TestViewData>) {
        mList = list
        notifyDataSetChanged()
    }

    fun updateData(list: List<TestViewData>) {
        if (mList == null) {
            mList = ArrayList<TestViewData>()
        } else {
            mList!!.clear()
        }
        mList?.addAll(list)
        notifyDataSetChanged()
    }
}